<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Log;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use Monolog\Handler\FirePHPHandler;
use Monolog\Formatter\LineFormatter;
use DB;
use \Exception;
use App\User;

class HomeController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
    	// dd(Log);
    	// Log::useFiles(public_path('/logs/laravel1.log'), 'error');
    	// Log::error('testErrorlog');
        // Log::log('debug', '这是调试信息', ['user'=> 'cpf']);
        // Log::useDailyFiles(storage_path('tmp.log'), 1);
        // Log:: error('新建日常日志处理器测试'); // 此日志会同时出现在tmp-xxxx-xx-xx.log和laravel.log两个文件中
        // Log::listen();
        
        // Log::error('测试修改context，增加通道', ['user'=>'test']);
        // $monolog = Log::getMonolog();
        // $monolog->error('更改通道');
        // dd($monolog);
        // Log::error('回调测试');
        // $log = Log::useSyslog();
        // $log->error('aaaaaaaaaaaaaa');
        // dd($log);
    	// Log::error('自定义日志路径实例222');
        // Log::setEventDispatcher('test');
        // dd(Log::getEventDispatcher());
        // 
        /**
         * php 异常处理demo
         *
         * 
         */
        $a = null;
        echo 456;
        // throw new Exception('实验外部跑');
        echo 123;
        try {
            // 对于这个错误竟然不用手动抛异常依然可以被catch捕获,所以此时手动抛出的异常不会被catch到，catch到的是默认的Exception的异常。这应该是一个优先级问题。当没有默认的异常抛出的情况下，catch到的是手动抛出的异常。
            // 或许是laravel框架辅助捕获了这个异常，因为脱离框架进行该实验的话，不手动抛出异常是无法捕获的。程序会直接报错终止运行
            // $a = 5/0;
            // echo $a;
            // require('1.php');
            // throw new Exception('实验外部跑');

            // 除 0 问题可以被异常类捕获了？？
            // throw new \Exception('除数不能是0');
            // User::testException();  // Non-static method App\User::testException() should not be called statically, assuming $this from incompatible context  像这种异常会直接被捕获，不会影响程序运行
            $user = new User();
            $user -> testException(); // 这个问题说明异常的抛出和捕获只有依托try...catch...才能很好地实现
        } catch (\exception $e) {
            echo  $e -> getMessage();
            // echo $a = -1;
            Log::warning(__DIR__ . __LINE__ . $e -> getMessage());
        } finally {
            // 不管成功失败都会执行的代码
            $a = -3;
        }
        $b = 4;
        echo $b;
        echo $a;

        
        
        
        
    	exit;
    	// abort(403, 'Unauthorized action.');
    	// Log::error('测试日志');
    	// Log::emergency('紧急状况');
    	// Log::info("用户xxx登录成功",['user_id'=>1]);
        // dd(2314);
        // Create the logger
		// $logger = new Logger('my_logger');
		// // Now add some handlers
		// $logger->pushHandler(new StreamHandler(__DIR__.'/my_app.log', Logger::DEBUG));
		// $logger->pushHandler(new FirePHPHandler());

		// // You can now use your logger
		// $logger->info('My logger is now ready');
		// Create some handlers
		
		$dateFormat = "Y n j, g:i a";
		// the default output format is "[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n"
		$output = "%datetime% > %level_name% > %message% %context% %extra%\n";
		// finally, create a formatter
		$formatter = new LineFormatter($output, $dateFormat);		

		$stream = new StreamHandler(__DIR__.'/my_app1.log', Logger::DEBUG);
		$stream->setFormatter($formatter);
		$firephp = new FirePHPHandler();

		// // Create the main logger of the app
		$logger = new Logger('my_logger');
		$logger->pushHandler($stream);
		$logger->pushHandler($firephp);

		// // Create a logger for the security-related stuff with a different channel
		// $securityLogger = new Logger('security');
		// $securityLogger->pushHandler($stream);
		// $securityLogger->pushHandler($firephp);

		// // Or clone the first one to only change the channel
		// $securityLogger = $logger->withName('security');

		// $logger->error("my_logger记录测试");
		// $securityLogger->error("security记录测试");
		// $res = DB::table('categories')->get();
		// DB::table('categories')->delete();
		// dd($res);
		// $monolog = Log::getMonolog();
		// $monolog -> info('调用底层实例');

		// the default date format is "Y-m-d H:i:s"
		
		$logger->info('这是测试格式化');

    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
